package aom.ju.ss.view.user

import android.view.View
import aom.ju.ss.R
import aom.ju.ss.databinding.ActivityEditAddressBinding
import aom.ju.ss.lpInterface.TitleDialogInterface
import aom.ju.ss.mode.ReceivingAddressData
import aom.ju.ss.mode.ReceivingAddressList
import aom.ju.ss.utils.VG
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.LP_API.DEL_ADDRESS
import aom.ju.ss.utils.http.LP_API.address_list
import aom.ju.ss.utils.provinceCityDistrict.ShowPCADialogUtils
import aom.ju.ss.utils.http.LP_API.save_address
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.provinceCityDistrict.CustomCityData
import aom.ju.ss.utils.provinceCityDistrict.OnCustomCityPickerItemClickListener
import aom.ju.ss.utils.show
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.dialog.TitleDialog
import com.google.gson.Gson

/**
 *添加 编辑 修改 收货地址
 */
class AddEditDelListActivity : BaseActivity() {
    private lateinit var binding: ActivityEditAddressBinding
    var data = "0"// 0 新增   addressid 修改/删除
    var upmode = UpDataMode()
    var mode: ReceivingAddressData? = null
    override fun init() {
        binding = viewBinding as ActivityEditAddressBinding
        binding.title.backImg.setImageResource(R.drawable.ic_back_black)
        binding.title.line
        data = intent.getStringExtra("data")!!
        binding.title.title.text = if (data == "0") "添加收货地址" else "编辑收货地址"
        binding.title.r_title.text = "保存"

        if (data != "0") {
            mode = Gson().fromJson(data, ReceivingAddressData::class.java)
            upmode.id = mode!!.id
            upmode.pro_id = mode!!.pro_id
            upmode.city_id = mode!!.city_id
            upmode.area_id = mode!!.area_id
            upmode.street_id = mode!!.street_id
            upmode.address = mode!!.address
            upmode.is_default = mode!!.is_default
            upmode.consignee = mode!!.consignee
            upmode.mobile = mode!!.mobile
            upmode.region_info = mode!!.region_info!!


            binding.ssq.text = mode!!.region_info
            binding.jd.text = mode!!.street_name

            binding.address.setText(mode!!.address)
            binding.del.VG(true)
            binding.del.setOnClickListener {
                TitleDialog().show(activity, object : TitleDialogInterface {
                    override fun ok(view: View?, IsDetermine: Boolean) {
                        if (IsDetermine) {
                            OKHttpUtils(activity)
                                .SetApiUrl(LP_API.DEL_ADDRESS)
                                .SetKey("id")
                                .SetValue(mode!!.id)
                                .POST(this@AddEditDelListActivity)
                        }

                    }

                }).setConnect("是否删除?")
                    .tvDetermine("删除")
                    .tvCancel("取消")

            }
        }


        ShowPCADialogUtils(
            this,
            mode?.area_id,
            binding.ssq,
            binding.jd,
            object : OnCustomCityPickerItemClickListener() {
                override fun onSelected(
                    province: CustomCityData,
                    city: CustomCityData,
                    district: CustomCityData
                ) {
                    super.onSelected(province, city, district)
                    upmode.pro_id = province.id
                    upmode.city_id = city.id
                    upmode.area_id = district.id

                }

                override fun onSelectedStreet(street_id: String, streetname: String?) {
                    upmode.street_id = street_id

                }
            })
        binding.info = upmode
        binding.title.r_title.setOnClickListener {
            if (upmode.consignee == "") {
                "请输入收货人姓名".show
                return@setOnClickListener
            }
            if (upmode.mobile == "") {
                "请输入手机号".show
                return@setOnClickListener
            }
            if (upmode.pro_id == "" || upmode.city_id == "" || upmode.area_id == "") {
                "请输入省市区信息".show
                return@setOnClickListener
            }
            if (upmode.street_id == "") {
                "请输入省市区信息".show
                return@setOnClickListener
            }
            if (upmode.address == "") {
                "请输入详细地址".show
                return@setOnClickListener
            }
            upmode.is_default = if (binding.def.isChecked) "1" else "0"


            OKHttpUtils(activity)
                .SetApiUrl(address_list)
                .POST(this)

            OKHttpUtils(activity)
                .SetApiUrl(save_address)
                .SetKey(
                    "pro_id",
                    "city_id",
                    "area_id",
                    "street_id",
                    "address",
                    "consignee",
                    "mobile",
                    "id",
                    "is_default"
                ).SetValue(
                    upmode.pro_id,
                    upmode.city_id,
                    upmode.area_id,
                    upmode.street_id,
                    upmode.address,
                    upmode.consignee,
                    upmode.mobile,
                    upmode.id,
                    upmode.is_default
                )
                .POST(this)
        }
        binding.def.isChecked = upmode.is_default == "1"
    }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        when (httpTY) {
            save_address -> {
                TitleDialog.showMsg(activity, "保存成功", object : TitleDialogInterface {
                    override fun ok(view: View?, IsDetermine: Boolean) {
                        setResult(RESULT_OK)
                        finish()
                    }
                })
            }
            address_list -> {
                val mode = Gson().fromJson(jsonString, ReceivingAddressList::class.java)
                mode.data.let {
                    if (it == null || it.isEmpty()) {
                        upmode.is_default = "1"
                    }
                }
            }
            DEL_ADDRESS -> {
                "删除成功".show
                setResult(RESULT_OK)
                finish()
            }
        }
    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, jsonString, httpTY)
        e.show
    }


    override val lay: Int = R.layout.activity_edit_address

    class UpDataMode {
        var pro_id: String = ""
        var city_id: String = ""
        var area_id: String = ""
        var street_id: String = ""
        var address: String = ""
        var consignee: String = ""
        var mobile: String = ""
        var id: String = ""
        var region_info: String = ""
        var is_default: String = ""//是否设为默认  1是   0否
    }
}